package com.pengheng.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
 * ResultVo 为响应对象
 *
 * @author pengheng
 */
@Data
@NoArgsConstructor
public class ResultVo<T> implements Serializable {

    private static final long serialVersionUID = -7535228184843182772L;

    /**
     * 返回的code
     */
    private Integer error_code;

    /**
     * 返回的信息
     */
    private String message;

    /**
     * 返回的结果集合
     */
    private List<T> rows;

    /**
     * 返回的单个对象
     */
    private T result;

    private Long total;

    private ResultVo(Integer error_code, String message) {
        this.error_code = error_code;
        this.message = Optional.ofNullable(message).orElse("");
    }

    private ResultVo(Integer error_code, String message, T result) {
        this.error_code = error_code;
        this.message = Optional.ofNullable(message).orElse("");
        this.result = result;
    }

    private ResultVo(Integer error_code, String message, List<T> rows, Long total) {
        this.error_code = error_code;
        this.message = Optional.ofNullable(message).orElse("");
        this.rows = rows;
        this.total = total;
    }

    public static <T> ResultVo<T> page(List<T> rows) {
        PageInfo<T> pageInfo = new PageInfo<>(rows);
        return new ResultVo<>(ResultCode.SUCCESS.getCode(), "获取数据成功", rows, pageInfo.getTotal());
    }

    public static <T> ResultVo<T> result(String message) {
        return result(message, null);
    }

    public static <T> ResultVo<T> result(String message, T t) {
        return new ResultVo<>(ResultCode.SUCCESS.getCode(), message, t);
    }

    public static <T> ResultVo<T> result(Integer code, String message, T t) {
        return new ResultVo<>(code, message, t);
    }


    public static <T> ResultVo<T> success() {
        return success("success");
    }


    public static <T> ResultVo<T> success(String message) {
        return new ResultVo(ResultCode.SUCCESS.getCode(), message);
    }

    public static <T> ResultVo<T> success(String message, List<T> rows) {
        return success(message, rows, null);
    }

    public static <T> ResultVo<T> success(String message, IPage<T> iPage) {
        return new ResultVo<>(ResultCode.SUCCESS.getCode(), message, iPage.getRecords(), iPage.getTotal());
    }

    public static <T> ResultVo<T> success(String message, List<T> rows, Long total) {
        return new ResultVo<>(ResultCode.SUCCESS.getCode(), message, rows, total);
    }

    public static ResultVo<Void> error() {
        return error("error");
    }

    public static ResultVo<Void> error(String message) {
        return new ResultVo<>(ResultCode.ERROR.getCode(), message);
    }

    public static ResultVo<Void> error(ResultCode resultCode) {
        return new ResultVo<>(resultCode.getCode(), resultCode.getMessage());
    }

    public static ResultVo<Void> error(Integer code, String message) {
        return new ResultVo<>(code, message);
    }

}